package com.cade.agriculturemall.view.personalCenter.selectAddress;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.cade.agriculturemall.R;
import com.cade.agriculturemall.base.BaseActivity;
import com.cade.agriculturemall.base.BaseAdapter;
import com.cade.agriculturemall.model.AddressEntity;
import com.cade.agriculturemall.view.personalCenter.manageAddress.ManageAddressActivity;

import java.util.List;

public class AddressSelectActivity extends BaseActivity<AddressSelectContract.IPresenter> implements AddressSelectContract.IView, View.OnClickListener {
    AddressSelectAdapter mAdapter = new AddressSelectAdapter(getApplication());

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_select);
        setupToolbar(true);
    }

    @Override
    public void getMyAddress(List<AddressEntity> list) {
        mAdapter.setEntity(list);
        mAdapter.notifyDataSetChanged();
        mAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intentBack = new Intent();
                intentBack.putExtra("addressId", mAdapter.getmEntityList().get(position).getObjectId());
                intentBack.putExtra("change", 1);
                setResult(RESULT_OK, intentBack);
                finish();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.downLoadMyAddress();
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.address_select_recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        recyclerView.setAdapter(mAdapter);
    }

    @Override
    public void setupView() {

    }

    @Override
    public void setupEvent() {

    }

    @Override
    public void setupData() {

    }

    @Override
    public AddressSelectContract.IPresenter setupPresenter() {
        return new AddressSelectPresenter(this);
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_manage, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.action_manage) {
            Intent intent = new Intent(this, ManageAddressActivity.class);
            startActivity(intent);
        }
        return super.onOptionsItemSelected(item);
    }
}
